package com.salelogistics.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salelogistics.common.core.utils.DateUtils;
import com.salelogistics.common.security.utils.SecurityUtils;
import com.salelogistics.sale.domain.SmsDeliveryPlan;
import com.salelogistics.sale.domain.SmsDeliveryPlanDetails;
import com.salelogistics.sale.domain.dto.DeliveryPlanDetailsDto;
import com.salelogistics.sale.domain.vo.DeliveryPlanVo;
import com.salelogistics.sale.mapper.SmsBatchPlanDetailsMapper;
import com.salelogistics.sale.mapper.SmsDeliveryPlanDetailsMapper;
import com.salelogistics.sale.mapper.SmsDeliveryPlanMapper;
import com.salelogistics.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.salelogistics.sale.mapper.SmsBatchPlanMapper;
import com.salelogistics.sale.domain.SmsBatchPlan;
import com.salelogistics.sale.service.ISmsBatchPlanService;

import static com.salelogistics.sale.service.impl.SmsDeliveryPlanServiceImpl.APPROVED;
import static com.salelogistics.sale.service.impl.SmsDeliveryPlanServiceImpl.DE_APPROVAL;

/**
 * 计划分批Service业务层处理
 *
 * @author Hu Kangying
 * @date 2023-07-21
 */
@Service
public class SmsBatchPlanServiceImpl extends ServiceImpl<SmsBatchPlanMapper, SmsBatchPlan> implements ISmsBatchPlanService {
    @Autowired
    private SmsBatchPlanMapper smsBatchPlanMapper;
    @Autowired
    private SmsBatchPlanDetailsMapper batchPlanDetailsMapper;
    @Autowired
    private SmsDeliveryPlanDetailsMapper deliveryPlanDetailsMapper;

    /**
     * 查询计划分批
     *
     * @param serialId 计划分批主键
     * @return 计划分批
     */
    @Override
    public SmsBatchPlan selectSmsBatchPlanBySerialId(Long serialId) {
        return smsBatchPlanMapper.selectSmsBatchPlanBySerialId(serialId);
    }

    /**
     * 查询计划分批列表
     *
     * @param smsBatchPlan 计划分批
     * @return 计划分批
     */
    @Override
    public List<SmsBatchPlan> selectSmsBatchPlanList(SmsBatchPlan smsBatchPlan) {
        return smsBatchPlanMapper.selectSmsBatchPlanList(smsBatchPlan);
    }

    /**
     * 新增计划分批
     *
     * @param smsBatchPlan 计划分批
     * @return 结果
     */
    @Override
    public int insertSmsBatchPlan(SmsBatchPlan smsBatchPlan) {
        smsBatchPlan.setCreateTime(DateUtils.getNowDate());
        return smsBatchPlanMapper.insertSmsBatchPlan(smsBatchPlan);
    }

    /**
     * 修改计划分批
     *
     * @param smsBatchPlan 计划分批
     * @return 结果
     */
    @Override
    public int updateSmsBatchPlan(SmsBatchPlan smsBatchPlan) {
        return smsBatchPlanMapper.updateSmsBatchPlan(smsBatchPlan);
    }

    /**
     * 批量删除计划分批
     *
     * @param serialIds 需要删除的计划分批主键
     * @return 结果
     */
    @Override
    public int deleteSmsBatchPlanBySerialIds(Long[] serialIds) {
        return smsBatchPlanMapper.deleteSmsBatchPlanBySerialIds(serialIds);
    }

    /**
     * 删除计划分批信息
     *
     * @param serialId 计划分批主键
     * @return 结果
     */
    @Override
    public int deleteSmsBatchPlanBySerialId(Long serialId) {
        return smsBatchPlanMapper.deleteSmsBatchPlanBySerialId(serialId);
    }

    @Override
    public List<DeliveryPlanDetailsDto> queryBatchPlanDetailsWithPlanId(Long planId) {
        if (planId == null){
            return null;
        }
        List<DeliveryPlanDetailsDto> planDetailsList = batchPlanDetailsMapper.
                selectBatchDetailsByBatchId(planId);
        //System.out.println("planDetailsList \n" + planDetailsList);
        //for (DeliveryPlanDetailsDto detailsDto : planDetailsList) {
        //    if (detailsDto.getCompletedCount() == null){
        //        detailsDto.setDecomposableCount(detailsDto.getPlanQuantity());
        //        detailsDto.setCompletedCount(BigDecimal.ZERO);
        //    }
        //}
        return planDetailsList;
    }

    @Override
    public int processBatchPlan(Long planId, boolean flag) {
        UpdateWrapper<SmsBatchPlan> updateWrapper = new UpdateWrapper<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        if (flag) {
            //审核
            updateWrapper.set("reviewed_status", APPROVED);
        } else {
            //反审核
            updateWrapper.set("reviewed_status", DE_APPROVAL);
        }
        updateWrapper.set("reviewed_by", userid).
                set("reviewed_time", DateUtils.getNowDate())
                .eq("serial_id", planId);
        boolean update = update(updateWrapper);
        return update ? 1 : 0;
    }
}
